﻿Ext.define('Oa.AssetGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'Widget.AssetGrid',

    // override
    initComponent: function () {
        this.columns = [
            {
                header: "编号", flex: 1,
                dataIndex: 'No'
            }, {
                header: "名称", width:120,
                dataIndex: 'Name'
            }, {
                header: "型号", width: 90,
                dataIndex: 'Model'
            }, {
                header: "规格", width: 90,
                dataIndex: 'Spec'
            }, {
                header: "单位", width: 90,
                dataIndex: 'Unit'
            }, {
                header: "价格", width: 90,
                dataIndex: 'Price'
            }, {
                header: "购买日期", width:120,
                dataIndex: 'BuyDate'
            }, {
                header: "保管人", width: 90,
                dataIndex: 'Keeper'
            }, {
                header: "状态", width: 90,
                dataIndex: 'Status'
            }, {
                header: "操作", dataIndex: 'No', width: 120, renderer: function (value, a, reader, c, d, ds) {
                    var links = [];
                    links.push('<a href="#" onclick="var url =\'AssetEdit.aspx?no=' + value + '\';window.showModalDialog(url, window, \'dialogWidth=850px;dialogHeight=550px;status=no;help=no;resizable=yes;\');Ext.getCmp(\'AssetMgr\').getStore().reload();">编辑</a>');
                    return links.join("&nbsp;");
                }
            }
        ];
        // Set up a model to use in our Store
            Ext.define('AssetModel', {
            extend: 'Ext.data.Model',
            fields: [{
                name: 'No'
            }, {
                name: 'Sn'
            }, {
                name: 'Name'
            }, {
                name: 'Class'
            }, {
                name: 'Model'
            }, {
                name: 'Spec'
            }, {
                name: 'Unit'
            }, {
                name: 'Supplier'
            }, {
                name: 'Price'
            }, {
                name: 'BuyDate'
            }, {
                name: 'Status'
            }, {
                name: 'KeeperId'
            }, {
                name: 'Keeper'
            }, {
                name: 'DistributeDate'
            }, {
                name: 'DueTime'
            }, {
                name: 'Remarks'
            }]
        });

        var ds = Ext.create('Ext.data.Store', {
            model: 'AssetModel',
            pageSize: 20, // items per page
            proxy: {
                type: 'ajax',
                url: '/Asset.mvc/Find',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'//分页使用
                }
            },
            autoLoad: false
        });
        ds.load({ params: { start: 0, limit: 20} });
        ds.on('beforeload', function (thiz, options) {
            var sDateOri = Ext.getCmp('sDateAsset').getValue();
            var eDateOri = Ext.getCmp('eDateAsset').getValue();
            var sDate = Ext.util.Format.date(sDateOri, 'Y-m-d');
            var eDate = Ext.util.Format.date(eDateOri, 'Y-m-d');
            Ext.apply(thiz.proxy.extraParams, { name: Ext.getCmp('txtNameAsset').getValue(),
                startDate: sDate,
                endDate: eDate
            });
        });
        this.store = ds;

        this.dockedItems = [{
            xtype: 'toolbar',
            dock: 'top',
            items: [{
                xtype: 'tbtext',
                text: '设备名称:'
            }, {
                xtype: 'textfield',
                id: 'txtNameAsset',
                emptyText: '设备的一或几个字',
                blankText: '设备的一或几个字',
                anchor: '100%'
            }, {
                xtype: 'tbtext',
                text: '购买日期(起):'
            }, {
                id: 'sDateAsset',
                xtype: 'datefield',
                format: 'Y/m/d'
            }, {
                xtype: 'tbtext',
                text: '购买日期(止):'
            }, {
                id: 'eDateAsset',
                xtype: 'datefield',
                format: 'Y/m/d'
            }, {
                text: "查询",
                iconCls: 'icon-search',
                handler: function () {
                    Ext.getCmp('pagingAsset').moveFirst();
                }
            }, '-', {
                text: "添加",
                tooltip: "添加新的信息",
                iconCls: "addicon",
                handler: function () {
                    var url = 'AssetEdit.aspx';
                    var retVal = window.showModalDialog(url, window, 'dialogWidth=850px;dialogHeight=550px;status=no;help=no;');
                    if (retVal != undefined && retVal == "Succeed") {
                        ds.reload();
                    }
                }
            }]
        }, {
            xtype: 'pagingtoolbar',
            id: 'pagingAsset',
            store: ds,
            dock: 'bottom',
            displayInfo: true
        }];

        // finally call the superclasses implementation
        Oa.AssetGrid.superclass.initComponent.call(this);
    }
});